// 文档相关 API 接口
import request from './request'
import type {
  CreateDocumentRequest,
  UpdateDocumentRequest,
  // DeleteDocumentRequest,
  // GetRecentDocumentsRequest,
  CreateDocumentResponse,
  UpdateDocumentResponse,
  GetDocumentResponse,
  DeleteDocumentResponse,
  UpdateDocumentNameResponse,
  CreateVersionRequest,
  DocumentVersion,
  DocumentVersionCompareResponse,
  GetRecentDocumentResponse,
  GetSharedDocumentResponse,
  GetDocCollaboratorResponse,
  User,
  // DeleteDocumentResponse,
  // GetRecentDocumentsResponse
} from '@/types/index'


// 新建文档
export const createDocument = (data: CreateDocumentRequest): Promise<CreateDocumentResponse> => {
  return request.post('/docs', data)
}

// 编辑文档
export const updateDocument = (data: UpdateDocumentRequest): Promise<UpdateDocumentResponse> => {
  return request.put(`/docs`, data)
}

// 获取文档详情
export const getDocumentDetail = (id: string, userId: string): Promise<GetDocumentResponse> => {
  return request.get(`/docs/${id}`, {
    headers: {
      'X-user-Id': userId
    }
  })
}

// 删除文档
export const deleteDocument = (id: string): Promise<DeleteDocumentResponse> => {
  return request.delete(`/docs/${id}`)
}

export const updateDocumentName = (id: string, name: string): Promise<UpdateDocumentNameResponse> => {
  return request.put(`/docs/${id}/${name}`)
}

// 获取最近访问文档
export const getRecentDocuments = (userId: string): Promise<GetRecentDocumentResponse[]> => {
  return request.get('/docs/recent', { headers: { 'X-user-Id': userId } })
}

// 获取共享文档
export const getSharedDocuments = (userId: string) : Promise<GetSharedDocumentResponse[]> => {
  return request.get('/docs/shared', { headers: { 'X-user-Id': userId } })
}

// 获取文档的协作者
export const getDocCollaborators = (docId: string) : Promise<GetDocCollaboratorResponse[]> => {
  return request.get(`/docs/permissions/${docId}`)
}

// 获取可授权的用户列表
export const getUserList = (userId: string) : Promise<User[]> => {
  return request.get(`/docs/chosen/userList`, { headers: { 'X-user-Id': userId } })
}
// ============= 文档版本控制相关接口 =============

// 创建文档版本 - POST /docs/{docId}/versions
export const createDocumentVersion = (docId: string, data: CreateVersionRequest, userId: string): Promise<DocumentVersion> => {
  return request.post(`/docs/${docId}/versions`, data, {
    ...(userId && { headers: { 'X-user-Id': userId } }) // 确保 headers 被传递
  })
}

// 获取文档版本列表 - GET /docs/{docId}/versions
export const getDocumentVersions = (docId: string): Promise<DocumentVersion[]> => {
  return request.get(`/docs/${docId}/versions`)
}

// 获取指定版本详情 - GET /docs/{docId}/versions/{versionNumber}
export const getDocumentVersionDetail = (docId: string, versionNumber: number): Promise<DocumentVersion> => {
  return request.get(`/docs/${docId}/versions/${versionNumber}`)
}

// 恢复到指定版本 - POST /docs/{docId}/versions/{versionNumber}/restore
export const restoreDocumentVersion = (docId: string, versionNumber: number, userId: string): Promise<string> => {
  return request.post(`/docs/${docId}/versions/${versionNumber}/restore`,{
    headers: {
      'X-user-Id': userId
    }
  })
}

// 删除指定版本 - DELETE /docs/{docId}/versions/{versionNumber}
export const deleteDocumentVersion = (docId: string, versionNumber: number): Promise<string> => {
  return request.delete(`/docs/${docId}/versions/${versionNumber}`)
}

// 版本对比 - GET /docs/{docId}/versions/compare
export const compareDocumentVersions = (docId: string, version1: number, version2: number): Promise<DocumentVersionCompareResponse> => {
  return request.get(`/docs/${docId}/versions/compare?version1=${version1}&version2=${version2}`)
}